<?php
include("../header.inc");
require_once("../DataBase.inc");

if (isset($_POST["province_id"]) && isset($_POST["interview_center_name"])) {
	if (trim($_POST["interview_center_name"])=="") echo "<div style='color:red'>The interview center cannot be created because you didn't enter a name.</div>";
	else {
		$r = DataBase::select("interview_center",null,array("province_id"=>$_POST["province_id"],"interview_center_name"=>$_POST["interview_center_name"]));
		if ($r <> false && count($r)>0) echo "<div style='color:red'>A center already exist with the same name in the same province!</div>";
		else DataBase::insert("interview_center",array("province_id"=>$_POST["province_id"],"interview_center_name"=>$_POST["interview_center_name"]));
	}
} else if (isset($_GET["import_exam_center"])) {
	$r = DataBase::select("exam_center",null,array("exam_center_id"=>$_GET["import_exam_center"]));
	if ($r <> false && count($r)==1) {
		$r = $r[0];
		$res = DataBase::select("interview_center",null,array("province_id"=>$r["province_id"],"interview_center_name"=>$r["exam_center_name"]));
		if ($res <> false && count($res)>0) echo "<div style='color:red'>A center already exist with the same name in the same province!</div>";
		else {
			$id = DataBase::insert("interview_center",array("province_id"=>$r["province_id"],"interview_center_name"=>$r["exam_center_name"]));
			if ($_GET["import_rooms"]=="yes") {
				$r = DataBase::select("exam_center_rooms", null, array("exam_center_id"=>$_GET["import_exam_center"]));
				if ($r <> false)
					foreach ($r as $room) DataBase::insert("interview_center_rooms",array("interview_center_id"=>$id,"room"=>$room["room"]));
			}
		}
	}
} else if (isset($_GET["remove"])) {
	if (!isset($_GET["confirm"])) {
		echo "Are you sure you want to remove this interview center, and all associated interviews ?<br/>";
		echo "<a href='?remove=".$_GET["remove"]."&confirm=yes'>Yes, remove it</a> &nbsp; &nbsp; <a href='?'>No, go back to the list</a>";
		die();
	}
	DataBase::delete("interview_center", array("interview_center_id"=>$_GET["remove"]));
	DataBase::delete("interview_center_rooms", array("interview_center_id"=>$_GET["remove"]));
	$interviews = DataBase::select("interview",null,array("interview_center_id"=>$_GET["remove"],"selection_campaign_id"=>$_SESSION["campaign_id"]));
	DataBase::delete("interview",array("interview_center_id"=>$_GET["remove"],"selection_campaign_id"=>$_SESSION["campaign_id"]));
	if ($interviews <> false)
		foreach ($interviews as $i) {
			DataBase::delete("interviewer",array("interview_id"=>$i["interview_id"]));
			DataBase::delete("interview_observer",array("interview_id"=>$i["interview_id"]));
			DataBase::update("applicant_interview",array("interview_id"=>$i["interview_id"]),array("interview_id"=>null));
		}
}

include "status.inc";
echo "<br/>";

$centers = DataBase::query("SELECT * FROM interview_center LEFT JOIN province ON province.province_id=interview_center.province_id ORDER BY province_name,interview_center_name");
if ($centers == false) $centers = array();
?>
List of interview centers:
<table border=1 style='border-collapse:collapse' cellpadding=2>
<tr>
 <th style='background-color:#C0C0C0'></th>
 <th style='background-color:#C0C0C0'>Province</th>
 <th style='background-color:#C0C0C0'>Interview Center</th>
 <th style='background-color:#C0C0C0'>Status</th>
 <th style='background-color:#C0C0C0'>Action</th>
</tr>
<?php
foreach ($centers as $center) {
	echo "<tr>";
	echo "<td><a href='?remove=".$center["interview_center_id"]."'><img src='".$_SESSION["root_url"]."/images/trash.gif' border=0/></a></td>";
	echo "<td>".$center["province_name"]."</td>";
	echo "<td>".$center["interview_center_name"]."</td>";
	

	$nb_assigned = DataBase::query("SELECT COUNT(*) FROM applicant_interview LEFT JOIN applicant ON applicant.applicant_id=applicant_interview.applicant_id WHERE applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.interview_center_id=".$center["interview_center_id"]);
	$nb_assigned = $nb_assigned[0][0];
	
	$nb_plan = DataBase::query("SELECT COUNT(*) FROM applicant_interview LEFT JOIN applicant ON applicant.applicant_id=applicant_interview.applicant_id WHERE applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.interview_center_id=".$center["interview_center_id"]." AND applicant_interview.interview_id IS NOT NULL");
	$nb_plan = $nb_plan[0][0];
	$nb_not_plan = $nb_assigned-$nb_plan;

	$nb_no_interviewer = DataBase::query("SELECT COUNT(*) FROM interview LEFT JOIN interviewer ON interviewer.interview_id=interview.interview_id WHERE interview.interview_center_id=".$center["interview_center_id"]." AND interview.selection_campaign_id=".$_SESSION["campaign_id"]." AND interviewer.interviewer_username IS NULL");
	$nb_no_interviewer = $nb_no_interviewer[0][0];
	
	echo "<td>";
	echo $nb_assigned." applicants assigned to this center<br/>";
	if ($nb_not_plan == 0)
		echo "<span style='color:green'>All applicants have an interview plan</span><br/>";
	else
		echo "<span style='color:red'>".$nb_not_plan." applicant(s) have no interview plan</span><br/>";
	if ($nb_no_interviewer == 0)
		echo "<span style='color:green'>All interviews have at least one interviewer</span><br/>";
	else
		echo "<span style='color:red'>".$nb_no_interviewer." interview(s) have no interviewer</span><br/>";
	echo "</td>";

	echo "<td><a href='center.php?center_id=".$center["interview_center_id"]."'>Specify rooms, Plan interviews, Assign applicants, interviewers and observers</a></td>";
	echo "</tr>";
}
?>
</table>
<a href='add_center.php'>Add a new interview center</a>
<?php
include("../footer.inc");
?>